﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Products;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace MangoMes.Write.API.Applications.ProductTyped
{
    public class ProductTypesEditHandler : IRequestHandler<ProductTypesEditCommand, ResultsDTO<ProductTypes>>
    {
        #region 仓储
        /// <summary>
        /// 仓储
        /// </summary>
        private readonly IBateRepository<ProductTypes> ProductTypesRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productTypesRepository">物料产品分类</param>
        public ProductTypesEditHandler(IBateRepository<ProductTypes> productTypesRepository)
        {
            ProductTypesRepository = productTypesRepository;
        }
        #endregion

        #region 物料产品分类 反填
        /// <summary>
        /// 物料产品分类 反填
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultsDTO<ProductTypes>> Handle(ProductTypesEditCommand request, CancellationToken cancellationToken)
        {
            //异常处理
            try
            {
                //创建DapperHelper对象 用于与数据库进行交互
                DapperHelper dapperHelper = new DapperHelper();
                //ResultsDTO<ProductTypes>对象result，并设置Code为 0
                ResultsDTO<ProductTypes> result = new ResultsDTO<ProductTypes>() { Code = 0 };

                //连接mySql
                string conn = "server=112.124.69.237;port=3306;uid=root;pwd=No6123666;database=MangoMesDB";

                //创建新的实例 pro
                ProductTypes pro = new ProductTypes();

                //sql语句
                string str = $"select * from producttypes where productTypeId = {request.ProductTypeId} ";
                //使用FirstOrDefault方法获取第一个结果（如果有）或默认值（如果没有结果），并将其赋值给result.Objects，即把查询到的产品类型数据存储在result对象中
                result.Objects = ProductTypesRepository.GetAll(str, pro, conn).FirstOrDefault();

                result.Message = "物料产品分类 反填成功";

                return result; //返回包含查询结果和消息的result对象
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

    }
}
